﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IntroduceCompany.Singleton;
using IntroduceCompany.Util;

namespace IntroduceCompany.Controllers
{
    [HandleError]
    [IntroduceCompany.Models.CheckModule(ConstantClass.Backend_BossEmail_MenuID)]
    public class BossEmailController : Controller
    {
        [Authorize(Roles = ConstantClass.autBossEmail)]
        public ActionResult EmailList(string flag, string id)
        {
            if (flag == "Create")
            {
                ViewData["Create"] = "true";
                ViewData["Edit"] = "false";
            }
            if (flag == "Edit")
            {
                ViewData["ID"] = id;
                ViewData["Edit"] = "true";
                ViewData["Create"] = "false";
            }
            return View("/Views/Backend/BossEmail/BossEmailManagement.aspx", DataSingleton.GetInstance().BossEmails);
        }

        [Authorize(Roles = ConstantClass.autBossEmail)]
        public ActionResult ChangeActive(int id, bool active)
        {
            DataSingleton.GetBossEmailModels().ChangeActive(id, active);
            return RedirectToAction("EmailList", "BossEmail");
        }

        [Authorize(Roles = ConstantClass.autBossEmail)]
        public ActionResult ChangeSSL(int id, bool ssl)
        {
            DataSingleton.GetBossEmailModels().ChangeSSL(id, ssl);
            return RedirectToAction("EmailList", "BossEmail");
        }

        [Authorize(Roles = ConstantClass.autBossEmail)]
        [ChildActionOnly]
        public ActionResult Edit(string id)
        {
            return View("/Views/Backend/BossEmail/EditBossEmail.ascx", DataSingleton.GetInstance().BossEmails.SingleOrDefault(u=>u.ID == Convert.ToInt16(id)));
        }

        [Authorize(Roles = ConstantClass.autBossEmail)]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(BossEmail bossEmail, String password)
        {
            if (ModelState.IsValid)
            {
                if (password != "")
                {
                    bossEmail.Pass = password.Trim();
                }
                else 
                {
                    bossEmail.Pass = Crypto.DecryptStringAES(bossEmail.Pass, ConstantClass._DecryptPass);
                }
                DataSingleton.GetBossEmailModels().EditBossEmail(bossEmail);
            }
            else
            {
                return RedirectToAction("EmailList", "BossEmail", new { flag = "Edit" });
            }
            return RedirectToAction("EmailList", "BossEmail");
        }

        [Authorize(Roles = ConstantClass.autBossEmail)]
        public ActionResult Delete(int id)
        {
            DataSingleton.GetBossEmailModels().DeleteBossEmail(id);
            return RedirectToAction("EmailList", "BossEmail");
        }

        [Authorize(Roles = ConstantClass.autBossEmail)]
        [ChildActionOnly]
        public ActionResult Create()
        {
            BossEmail boss = new BossEmail();
            boss.Email = " ";
            boss.Pass = " ";
            return View("/Views/Backend/BossEmail/CreateBossEmail.ascx", boss);
        }

        [Authorize(Roles = ConstantClass.autBossEmail)]
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(BossEmail bossEmail)
        {
            if (ModelState.IsValid)
            {
                DataSingleton.GetBossEmailModels().CreateBossEmail(bossEmail);
                return RedirectToAction("EmailList", "BossEmail");
            }
            else {
                return RedirectToAction("EmailList", "BossEmail",new { flag = "Create" });
            }
        }
    }
}
